package com.jjc.android.activity

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import com.allen.library.SuperButton
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.entity.DetectionBackEvent
import com.jjc.android.entity.ForgetPasswordSuccessEvent
import com.jjc.android.entity.LzyResponse
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.paramEx
import com.jjc.android.view.CommonKt
import com.jjc.android.view.onClick
import com.jjc.android.view.textChange
import com.lzy.okgo.OkGo
import com.xw.repo.XEditText
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ModifyPasswordActivity : BaseActivity(){

    override val layoutId: Int = R.layout.activity_modify_password
    private val okBtn: SuperButton by lazy { findViewById<SuperButton>(R.id.okBtn) }
    private val forgetPwdBtn: TextView by lazy { findViewById<TextView>(R.id.forgetPwdBtn) }
    private val passwordRepeatEt: XEditText by lazy { findViewById<XEditText>(R.id.passwordRepeatEt) }
    private val passwordEt: XEditText by lazy { findViewById<XEditText>(R.id.passwordEt) }
    private val oldPasswordEt: XEditText by lazy { findViewById<XEditText>(R.id.oldPasswordEt) }

    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.alter_pwd_txt))
        forgetPwdBtn.onClick {
            startActivity(Intent(this,ForgetPasswordActivity::class.java))
        }
        okBtn.onClick {

            if(oldPasswordEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_old_pwd_txt))
                return@onClick
            }
            if(!CommonKt.isPasswordValid(oldPasswordEt.text.toString())){
                ToastUtils.show(getString(R.string.input_right_old_pwd_txt))
                return@onClick
            }
            if(passwordEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.input_new_pwd_txt))
                return@onClick
            }
            if(passwordRepeatEt.text.toString().isNullOrEmpty()){
                ToastUtils.show(getString(R.string.re_input_new_pwd_txt))
                return@onClick
            }
            if(!CommonKt.isPasswordValid(passwordEt.text.toString())){
                ToastUtils.show(getString(R.string.input_right_new_pwd_txt))
                return@onClick
            }
            if(!CommonKt.isPasswordValid(passwordRepeatEt.text.toString())){
                ToastUtils.show(getString(R.string.re_input_right_new_pwd_txt))
                return@onClick
            }
            if(passwordEt.text.toString()==oldPasswordEt.text.toString()){
                ToastUtils.show(getString(R.string.new_old_pwd_not_same_txt))
                return@onClick
            }
            if(passwordEt.text.toString()!=passwordRepeatEt.text.toString()){
                ToastUtils.show(getString(R.string.pwd_not_same_txt))
                return@onClick
            }
            OkGo.post<LzyResponse<String>>(Constants.updateOldPwd)
                .paramEx(mutableMapOf(
                    "password" to passwordEt.textEx,
                    "confirmPassword" to passwordRepeatEt.textEx,
                    "oldPassword" to oldPasswordEt.textEx
                ))
                .tag(this@ModifyPasswordActivity)
                .execute(object : SimpleHttpCallback<LzyResponse<String>>(activity = this){
                    override fun onCallSuccess(response: LzyResponse<String>) {
                        ToastUtils.show(getString(R.string.alter_pwd_success_txt))
                        finish()
                    }
                })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ForgetPasswordSuccessEvent){
        finish()
    }

    override fun isHaveEventBus(): Boolean {
        return true
    }
}